import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import { viteMockServe } from 'vite-plugin-mock';

// https://vite.dev/config/
export default defineConfig(({ command }) => {
  const isDev = command === 'serve';
  const useMock = isDev && process.env.VITE_USE_MOCK === 'true';

  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [AntDesignVueResolver()],
      }),
      Components({
        resolvers: [AntDesignVueResolver({ importStyle: 'less' })],
      }),
      viteMockServe({
        // 自动读取模拟数据
        mockPath: 'mock',
        // 根据环境和环境变量决定是否启用 Mock
        enable: useMock,
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          // 这里可以添加 Less 的全局变量、混合等配置
          // modifyVars: {
          //   // 示例：修改 Ant Design Vue 的主题颜色
          //   'primary-color': '#1DA57A',
          // },
          javascriptEnabled: true,
        },
      },
    },
  };
});